﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;

namespace Aliyeye.Car.Net.Dal
{
    public class Customer
    {
        public Customer()
        { }
        public int Add(Model.Customer model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Code", SqlDbType.NVarChar,50),
					new SqlParameter("@Cost", SqlDbType.Decimal,9),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Sex", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Tel", SqlDbType.NVarChar,50),
					new SqlParameter("@Addr", SqlDbType.NVarChar,200),
					new SqlParameter("@ComName", SqlDbType.NVarChar,150),
					new SqlParameter("@CodeCar", SqlDbType.NVarChar,50),
					new SqlParameter("@BondsMan", SqlDbType.NVarChar,50),
					new SqlParameter("@BondSex", SqlDbType.NVarChar,50),
					new SqlParameter("@BondTel", SqlDbType.NVarChar,50),
					new SqlParameter("@BondAddr", SqlDbType.NVarChar,200),
					new SqlParameter("@BondComName", SqlDbType.NVarChar,150),
					new SqlParameter("@BondCar", SqlDbType.NVarChar,50),
					new SqlParameter("@Relation", SqlDbType.NVarChar,50),
					new SqlParameter("@Company", SqlDbType.NVarChar,100),
					new SqlParameter("@CompAddr", SqlDbType.NVarChar,200),
					new SqlParameter("@LegalPer", SqlDbType.NVarChar,50),
					new SqlParameter("@ComCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Capital", SqlDbType.NVarChar,50),
					new SqlParameter("@OrgCode", SqlDbType.NChar,10),
					new SqlParameter("@PaidIn", SqlDbType.Decimal,9),
					new SqlParameter("@Owing", SqlDbType.Decimal,9),
					new SqlParameter("@Path", SqlDbType.NVarChar,50),
					new SqlParameter("@IsState", SqlDbType.Bit,1),
					new SqlParameter("@Custype", SqlDbType.Int,4),
					new SqlParameter("@Marks", SqlDbType.NVarChar,500),
					new SqlParameter("@InpuTime", SqlDbType.DateTime),
					new SqlParameter("@InputUserId", SqlDbType.Int,4),
					new SqlParameter("@Driver", SqlDbType.NVarChar,50),
					new SqlParameter("@DriSex", SqlDbType.NVarChar,50),
					new SqlParameter("@DriCode", SqlDbType.NVarChar,50),
					new SqlParameter("@DriTel", SqlDbType.NVarChar,50),
					new SqlParameter("@DriAddr", SqlDbType.NVarChar,150),
					new SqlParameter("@DriCard", SqlDbType.NVarChar,50),
					new SqlParameter("@CardTime", SqlDbType.DateTime),
					new SqlParameter("@OnDri", SqlDbType.DateTime),
					new SqlParameter("@JionCard", SqlDbType.NVarChar,50),
					new SqlParameter("@CarType", SqlDbType.NVarChar,50),
					new SqlParameter("@CareTime", SqlDbType.DateTime),
					new SqlParameter("@GetCar", SqlDbType.DateTime),
					new SqlParameter("@ComAddr", SqlDbType.NVarChar,200),
					new SqlParameter("@CusCartype", SqlDbType.NVarChar,50),
					new SqlParameter("@CustCar", SqlDbType.DateTime),
                    new SqlParameter("@pictureUser", SqlDbType.Image),
                    new SqlParameter("@pictureID", SqlDbType.Image),
                    new SqlParameter("@pictureDriveLicense", SqlDbType.Image),
                    new SqlParameter("@pictureAccount", SqlDbType.Image),
                    new SqlParameter("@pictureCreditCard", SqlDbType.Image),
                    new SqlParameter("@pictureBusinessLicense", SqlDbType.Image),
                    new SqlParameter("@pictureTax", SqlDbType.Image),
                    new SqlParameter("@UserID", SqlDbType.Int,4)
                                        };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.Cost;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.Sex;
            parameters[5].Value = model.Phone;
            parameters[6].Value = model.Tel;
            parameters[7].Value = model.Addr;
            parameters[8].Value = model.ComName;
            parameters[9].Value = model.CodeCar;
            parameters[10].Value = model.BondsMan;
            parameters[11].Value = model.BondSex;
            parameters[12].Value = model.BondTel;
            parameters[13].Value = model.BondAddr;
            parameters[14].Value = model.BondComName;
            parameters[15].Value = model.BondCar;
            parameters[16].Value = model.Relation;
            parameters[17].Value = model.Company;
            parameters[18].Value = model.CompAddr;
            parameters[19].Value = model.LegalPer;
            parameters[20].Value = model.ComCode;
            parameters[21].Value = model.Capital;
            parameters[22].Value = model.OrgCode;
            parameters[23].Value = model.PaidIn;
            parameters[24].Value = model.Owing;
            parameters[25].Value = model.Path;
            parameters[26].Value = model.IsState;
            parameters[27].Value = model.Custype;
            parameters[28].Value = model.Marks;
            parameters[29].Value = model.InpuTime;
            parameters[30].Value = model.InputUserId;
            parameters[31].Value = model.Driver;
            parameters[32].Value = model.DriSex;
            parameters[33].Value = model.DriCode;
            parameters[34].Value = model.DriTel;
            parameters[35].Value = model.DriAddr;
            parameters[36].Value = model.DriCard;
            parameters[37].Value = model.CardTime;
            parameters[38].Value = model.OnDri;
            parameters[39].Value = model.JionCard;
            parameters[40].Value = model.CarType;
            parameters[41].Value = model.CareTime;
            parameters[42].Value = model.GetCar;
            parameters[43].Value = model.ComAddr;
            parameters[44].Value = model.CusCartype;
            parameters[45].Value = model.CustCar;
            parameters[46].Value = model.pictureUser;
            parameters[47].Value = model.pictureID;
            parameters[48].Value = model.pictureDriveLicense;
            parameters[49].Value = model.pictureAccount;
            parameters[50].Value = model.pictureCreditCard;
            parameters[51].Value = model.pictureBusinessLicense;
            parameters[52].Value = model.pictureTax;
            parameters[53].Value = model.UserID;

            int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Customer_ADD", parameters);
            if (roleId > 0)
            {
                return roleId;
            }
            else
            {
                return 0;
            }
        }
        public bool Update(Model.Customer model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Code", SqlDbType.NVarChar,50),
					new SqlParameter("@Cost", SqlDbType.Decimal,9),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Sex", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Tel", SqlDbType.NVarChar,50),
					new SqlParameter("@Addr", SqlDbType.NVarChar,200),
					new SqlParameter("@ComName", SqlDbType.NVarChar,150),
					new SqlParameter("@CodeCar", SqlDbType.NVarChar,50),
					new SqlParameter("@BondsMan", SqlDbType.NVarChar,50),
					new SqlParameter("@BondSex", SqlDbType.NVarChar,50),
					new SqlParameter("@BondTel", SqlDbType.NVarChar,50),
					new SqlParameter("@BondAddr", SqlDbType.NVarChar,200),
					new SqlParameter("@BondComName", SqlDbType.NVarChar,150),
					new SqlParameter("@BondCar", SqlDbType.NVarChar,50),
					new SqlParameter("@Relation", SqlDbType.NVarChar,50),
					new SqlParameter("@Company", SqlDbType.NVarChar,100),
					new SqlParameter("@CompAddr", SqlDbType.NVarChar,200),
					new SqlParameter("@LegalPer", SqlDbType.NVarChar,50),
					new SqlParameter("@ComCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Capital", SqlDbType.NVarChar,50),
					new SqlParameter("@OrgCode", SqlDbType.NChar,10),
					new SqlParameter("@PaidIn", SqlDbType.Decimal,9),
					new SqlParameter("@Owing", SqlDbType.Decimal,9),
					new SqlParameter("@Path", SqlDbType.NVarChar,50),
					new SqlParameter("@IsState", SqlDbType.Bit,1),
					new SqlParameter("@Custype", SqlDbType.Int,4),
					new SqlParameter("@Marks", SqlDbType.NVarChar,500),
					new SqlParameter("@InpuTime", SqlDbType.DateTime),
					new SqlParameter("@InputUserId", SqlDbType.Int,4),
					new SqlParameter("@Driver", SqlDbType.NVarChar,50),
					new SqlParameter("@DriSex", SqlDbType.NVarChar,50),
					new SqlParameter("@DriCode", SqlDbType.NVarChar,50),
					new SqlParameter("@DriTel", SqlDbType.NVarChar,50),
					new SqlParameter("@DriAddr", SqlDbType.NVarChar,150),
					new SqlParameter("@DriCard", SqlDbType.NVarChar,50),
					new SqlParameter("@CardTime", SqlDbType.DateTime),
					new SqlParameter("@OnDri", SqlDbType.DateTime),
					new SqlParameter("@JionCard", SqlDbType.NVarChar,50),
					new SqlParameter("@CarType", SqlDbType.NVarChar,50),
					new SqlParameter("@CareTime", SqlDbType.DateTime),
					new SqlParameter("@GetCar", SqlDbType.DateTime),
					new SqlParameter("@ComAddr", SqlDbType.NVarChar,200),
					new SqlParameter("@CusCartype", SqlDbType.NVarChar,50),
					new SqlParameter("@CustCar", SqlDbType.DateTime),
                    new SqlParameter("@pictureUser", SqlDbType.Image),
                    new SqlParameter("@pictureID", SqlDbType.Image),
                    new SqlParameter("@pictureDriveLicense", SqlDbType.Image),
                    new SqlParameter("@pictureAccount", SqlDbType.Image),
                    new SqlParameter("@pictureCreditCard", SqlDbType.Image),
                    new SqlParameter("@pictureBusinessLicense", SqlDbType.Image),
                    new SqlParameter("@pictureTax", SqlDbType.Image),
                    new SqlParameter("@UserID", SqlDbType.Int,4)
                };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.Cost;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.Sex;
            parameters[5].Value = model.Phone;
            parameters[6].Value = model.Tel;
            parameters[7].Value = model.Addr;
            parameters[8].Value = model.ComName;
            parameters[9].Value = model.CodeCar;
            parameters[10].Value = model.BondsMan;
            parameters[11].Value = model.BondSex;
            parameters[12].Value = model.BondTel;
            parameters[13].Value = model.BondAddr;
            parameters[14].Value = model.BondComName;
            parameters[15].Value = model.BondCar;
            parameters[16].Value = model.Relation;
            parameters[17].Value = model.Company;
            parameters[18].Value = model.CompAddr;
            parameters[19].Value = model.LegalPer;
            parameters[20].Value = model.ComCode;
            parameters[21].Value = model.Capital;
            parameters[22].Value = model.OrgCode;
            parameters[23].Value = model.PaidIn;
            parameters[24].Value = model.Owing;
            parameters[25].Value = model.Path;
            parameters[26].Value = model.IsState;
            parameters[27].Value = model.Custype;
            parameters[28].Value = model.Marks;
            parameters[29].Value = model.InpuTime;
            parameters[30].Value = model.InputUserId;
            parameters[31].Value = model.Driver;
            parameters[32].Value = model.DriSex;
            parameters[33].Value = model.DriCode;
            parameters[34].Value = model.DriTel;
            parameters[35].Value = model.DriAddr;
            parameters[36].Value = model.DriCard;
            parameters[37].Value = model.CardTime;
            parameters[38].Value = model.OnDri;
            parameters[39].Value = model.JionCard;
            parameters[40].Value = model.CarType;
            parameters[41].Value = model.CareTime;
            parameters[42].Value = model.GetCar;
            parameters[43].Value = model.ComAddr;
            parameters[44].Value = model.CusCartype;
            parameters[45].Value = model.CustCar;
            parameters[46].Value = model.pictureUser;
            parameters[47].Value = model.pictureID;
            parameters[48].Value = model.pictureDriveLicense;
            parameters[49].Value = model.pictureAccount;
            parameters[50].Value = model.pictureCreditCard;
            parameters[51].Value = model.pictureBusinessLicense;
            parameters[52].Value = model.pictureTax;
            parameters[53].Value = model.UserID;

            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Customer_Update", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        public bool Delete(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Customer_Delete", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        public Model.Customer GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            Model.Customer model = new Model.Customer();
            DataSet ds = SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure, "Customer_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                
                model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
               
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.Sex = ds.Tables[0].Rows[0]["Sex"].ToString();
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.Tel = ds.Tables[0].Rows[0]["Tel"].ToString();
                model.Addr = ds.Tables[0].Rows[0]["Addr"].ToString();
                model.ComName = ds.Tables[0].Rows[0]["ComName"].ToString();
                model.CodeCar = ds.Tables[0].Rows[0]["CodeCar"].ToString();
                model.BondsMan = ds.Tables[0].Rows[0]["BondsMan"].ToString();
                model.BondSex = ds.Tables[0].Rows[0]["BondSex"].ToString();
                model.BondTel = ds.Tables[0].Rows[0]["BondTel"].ToString();
                model.BondAddr = ds.Tables[0].Rows[0]["BondAddr"].ToString();
                model.BondComName = ds.Tables[0].Rows[0]["BondComName"].ToString();
                model.BondCar = ds.Tables[0].Rows[0]["BondCar"].ToString();
                model.Relation = ds.Tables[0].Rows[0]["Relation"].ToString();
                model.Company = ds.Tables[0].Rows[0]["Company"].ToString();
                model.CompAddr = ds.Tables[0].Rows[0]["CompAddr"].ToString();
                model.LegalPer = ds.Tables[0].Rows[0]["LegalPer"].ToString();
                model.ComCode = ds.Tables[0].Rows[0]["ComCode"].ToString();
                model.Capital = ds.Tables[0].Rows[0]["Capital"].ToString();
                model.OrgCode = ds.Tables[0].Rows[0]["OrgCode"].ToString();
                if (ds.Tables[0].Rows[0]["PaidIn"].ToString() != "")
                {
                    model.PaidIn = decimal.Parse(ds.Tables[0].Rows[0]["PaidIn"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Owing"].ToString() != "")
                {
                    model.Owing = decimal.Parse(ds.Tables[0].Rows[0]["Owing"].ToString());
                }
                if(ds.Tables[0].Rows[0]["Cost"].ToString()!="")
                {
                    model.Cost = decimal.Parse(ds.Tables[0].Rows[0]["Cost"].ToString());
                }
                model.Path = ds.Tables[0].Rows[0]["Path"].ToString();
                if (ds.Tables[0].Rows[0]["IsState"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsState"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsState"].ToString().ToLower() == "true"))
                    {
                        model.IsState = true;
                    }
                    else
                    {
                        model.IsState = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Custype"].ToString() != "")
                {
                    model.Custype = int.Parse(ds.Tables[0].Rows[0]["Custype"].ToString());
                }
                model.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();
                if (ds.Tables[0].Rows[0]["InpuTime"].ToString() != "")
                {
                    model.InpuTime = DateTime.Parse(ds.Tables[0].Rows[0]["InpuTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InputUserId"].ToString() != "")
                {
                    model.InputUserId = int.Parse(ds.Tables[0].Rows[0]["InputUserId"].ToString());
                }
                model.Driver = ds.Tables[0].Rows[0]["Driver"].ToString();
                model.DriSex = ds.Tables[0].Rows[0]["DriSex"].ToString();
                model.DriCode = ds.Tables[0].Rows[0]["DriCode"].ToString();
                model.DriTel = ds.Tables[0].Rows[0]["DriTel"].ToString();
                model.DriAddr = ds.Tables[0].Rows[0]["DriAddr"].ToString();
                model.DriCard = ds.Tables[0].Rows[0]["DriCard"].ToString();
                if (ds.Tables[0].Rows[0]["CardTime"].ToString() != "")
                {
                    model.CardTime = DateTime.Parse(ds.Tables[0].Rows[0]["CardTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OnDri"].ToString() != "")
                {
                    model.OnDri = DateTime.Parse(ds.Tables[0].Rows[0]["OnDri"].ToString());
                }
                model.JionCard = ds.Tables[0].Rows[0]["JionCard"].ToString();
                model.CarType = ds.Tables[0].Rows[0]["CarType"].ToString();
                if (ds.Tables[0].Rows[0]["CareTime"].ToString() != "")
                {
                    model.CareTime = DateTime.Parse(ds.Tables[0].Rows[0]["CareTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["GetCar"].ToString() != "")
                {
                    model.GetCar = DateTime.Parse(ds.Tables[0].Rows[0]["GetCar"].ToString());
                }
                model.ComAddr = ds.Tables[0].Rows[0]["ComAddr"].ToString();
                model.CusCartype = ds.Tables[0].Rows[0]["CusCartype"].ToString();
                if (ds.Tables[0].Rows[0]["CustCar"].ToString() != "")
                {
                    model.CustCar = DateTime.Parse(ds.Tables[0].Rows[0]["CustCar"].ToString());
                }

                model.pictureUser = ds.Tables[0].Rows[0]["pictureUser"] == DBNull.Value ? null : (byte[])ds.Tables[0].Rows[0]["pictureUser"];
                model.pictureID = ds.Tables[0].Rows[0]["pictureID"] == DBNull.Value ? null : (byte[])ds.Tables[0].Rows[0]["pictureID"];
                model.pictureDriveLicense = ds.Tables[0].Rows[0]["pictureDriveLicense"] == DBNull.Value ? null : (byte[])ds.Tables[0].Rows[0]["pictureDriveLicense"];
                model.pictureAccount = ds.Tables[0].Rows[0]["pictureAccount"] == DBNull.Value ? null : (byte[])ds.Tables[0].Rows[0]["pictureAccount"];
                model.pictureCreditCard = ds.Tables[0].Rows[0]["pictureCreditCard"] == DBNull.Value ? null : (byte[])ds.Tables[0].Rows[0]["pictureCreditCard"];
                model.pictureBusinessLicense = ds.Tables[0].Rows[0]["pictureBusinessLicense"] == DBNull.Value ? null : (byte[])ds.Tables[0].Rows[0]["pictureBusinessLicense"];
                model.pictureTax = ds.Tables[0].Rows[0]["pictureTax"] == DBNull.Value ? null : (byte[])ds.Tables[0].Rows[0]["pictureTax"];

                if (ds.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
                }
                model.LoginUserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                if (ds.Tables[0].Rows[0]["Points"].ToString() != "")
                {
                    model.Points = Int64.Parse(ds.Tables[0].Rows[0]["Points"].ToString());
                }
                else
                {
                    model.Points = 0;
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Code,Cost,Name,Sex,Phone,Tel,Addr,ComName,CodeCar,BondsMan,BondSex,BondTel,BondAddr,BondComName,BondCar,Relation,Company,CompAddr,LegalPer,ComCode,Capital,OrgCode,PaidIn,Owing,Path,IsState,Custype=case when Custype=2 then '企业客户' else '个人客户' end,Marks,InpuTime,InputUserId,Driver,DriSex,DriCode,DriTel,DriAddr,DriCard,CardTime,OnDri,JionCard,CarType,CareTime,GetCar,ComAddr,CusCartype,CustCar,UserID,Points ");
            strSql.Append(" FROM Customer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.Query(strSql.ToString());
        }

    }
}
